package com.change.zh.ffmpegdo.controller;


import com.change.zh.ffmpegdo.config.StreamPlayYml;
import com.change.zh.ffmpegdo.entity.FetchRoom;
import com.change.zh.ffmpegdo.push.FFmpegManager;
import com.change.zh.ffmpegdo.push.JobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@RestController
public class CmdController {


    @Autowired
    private StreamPlayYml streamPlayYml;

    @RequestMapping(value = "alljob")
    public List<String> allJob() {
        List<String> jt = new ArrayList<>();
        //-------
        FFmpegManager fFmpegManager = new FFmpegManager();
        Collection<JobDetail> list = fFmpegManager.queryAll();
        Iterator<JobDetail> iter = list.iterator();
        JobDetail jobDetail = null;
        while (iter.hasNext()) {
            jobDetail = iter.next();
            jt.add(jobDetail.toString());
        }
        return jt;
    }

    @GetMapping(value = "closeall")
    public String cloaseAllJob() {
        FFmpegManager fFmpegManager = new FFmpegManager();
        return fFmpegManager.stopAll() + "";
    }

    @GetMapping(value = "close")
    public String cloaseJob(@RequestParam(value = "roomid") String roomid, @RequestParam(value = "roomtype") String roomtype) {
        FFmpegManager fFmpegManager = new FFmpegManager();
        boolean flag = fFmpegManager.stop(roomid + roomtype);
        return flag == true ? "{\"code\":\"成功\"}" : "{\"code\":\"失败\"}";
    }

    @PostMapping(value = "fetchclose")
    public String fetchCloaseJob(@RequestBody FetchRoom fetchRoom) {
        FFmpegManager fFmpegManager = new FFmpegManager();
        boolean flag = fFmpegManager.stop(fetchRoom.getRoomid() + fetchRoom.getRoomtype());
        return flag == true ? "{\"code\":\"成功\"}" : "{\"code\":\"失败\"}";
    }


    @GetMapping(value = "hpush")
    public String hlsJob(@RequestParam(value = "roomid") String roomid) {
        FFmpegManager fFmpegManager = new FFmpegManager();
        String message = "";
        if (fFmpegManager.query(roomid + "hls") == null) {
            message = fFmpegManager.start(roomid, "hls", "ffmpeg -re -i " +
                    streamPlayYml.getFfmpeg().getStreampath().get(1) + " -vcodec " +
                    "libx264 -acodec aac -strict -2 -f flv " +
                    "rtmp://" + streamPlayYml.getNginx().getIp() + ":" + streamPlayYml.getNginx().getPort() + "/" + streamPlayYml.getNginx().getHlsname() + "/" + roomid + "?pass=123456");
        } else {
            message = roomid + "hls已经在直播了";
        }
        return message;
    }

    @GetMapping(value = "rpush")
    public String rtmpJob(@RequestParam(value = "roomid") String roomid) {
        FFmpegManager fFmpegManager = new FFmpegManager();
        String message = "";
        if (fFmpegManager.query(roomid + "rtmp") == null) {
            message = fFmpegManager.start(roomid, "rtmp", "ffmpeg -re -i " +
                    streamPlayYml.getFfmpeg().getStreampath().get(0) + " -vcodec " +
                    "libx264 -acodec aac -strict -2 -f flv " +
                    "rtmp://" + streamPlayYml.getNginx().getIp() + ":" + streamPlayYml.getNginx().getPort() + "/" + streamPlayYml.getNginx().getLivename() + "/" + roomid + "?pass=123456");
        } else {
            message = roomid + "rtmp已经在直播了";
        }
        return message;
    }


}
